package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.swing.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@SuppressWarnings("all")
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    /**
     *商品搜索
     * ?category3Id=61&props=23:4G:运行内存&props=24:128G:机身内存&order=2:desc
     * @return
     */
    @GetMapping("list.html")
    public String list(SearchParam searchParam, Model model){

        //远程调用获取搜索结果
        Result<Map> result = listFeignClient.search(searchParam);
        //设置到页面
        model.addAllAttributes(result.getData());

        //回显搜索内容
        model.addAttribute("searchParam",searchParam);

        //拼接urlParam
        String urlParam=makeUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);

        //拼接品牌面包屑
        String trademarkParam =markTradeMark(searchParam.getTrademark());

        model.addAttribute("trademarkParam",trademarkParam);

        //拼接平台属性面包屑
        List<Map> propsParamList=markAttrsProp(searchParam.getProps());

        model.addAttribute("propsParamList",propsParamList);

        //排除处理
        Map<String,Object> sortMap=makeSortMap(searchParam.getOrder());

        model.addAttribute("orderMap",sortMap);


        return "list/index";
    }

    /**
     * 处理排序方式
     * @param order
     * @return
     */
    private Map<String, Object> makeSortMap(String order) {

        Map<String, Object> orderMap=new HashMap<>();

        //order=2:desc
        //判断
        if(!StringUtils.isEmpty(order)){

            String[] split = order.split(":");
            //判断
            if(split!=null &&split.length==2){

                orderMap.put("type",split[0]);
                orderMap.put("sort",split[1]);
            }


        }else {
            //默认根据热度排序
            //1：hotscore   2:pridct
            orderMap.put("type","1");
            orderMap.put("sort","desc");


        }

        return orderMap;
    }

    /**
     * 拼接平台属性面包屑
     * @param props
     * @return
     */
    private List<Map> markAttrsProp(String[] props) {

        List<Map> mapList=new ArrayList<>();
        //判断
        if(props!=null &&props.length>0){
            for (String prop : props) {

                //截取
                String[] split = prop.split(":");
                //判断
                if(split!=null &&split.length==3){

                    // 24:256G:机身存储
                    Map<String,Object> propMap=new HashMap<>();
                    propMap.put("attrId",split[0]);
                    propMap.put("attrName",split[2]);
                    propMap.put("attrValue",split[1]);

                    mapList.add(propMap);
                }


            }


        }


        return mapList;
    }

    /**
     * 品牌面包屑
     * @param trademark
     * @return
     * trademark=3:华为
     */
    private String markTradeMark(String trademark) {

        //trademark=3:华为
        //判断
        if(!StringUtils.isEmpty(trademark)){

            //截取字符串
            String[] split = trademark.split(":");
            //判断
            if(split!=null &split.length==2){

                return "品牌："+split[1];

            }


        }

        return "";
    }

    /**
     * 拼接urlParam路径
     *  http://list.gmall.com/list.html?category1Id=2
     *  http://list.gmall.com/list.html?keyword=手机
     *
     * @param searchParam
     * @return
     */
    private String makeUrlParam(SearchParam searchParam) {

        //创建构建对象
        StringBuilder builder=new StringBuilder();
        //判断关键字keyword
        String keyword = searchParam.getKeyword();
        //判断
        if(!StringUtils.isEmpty(keyword)){

            builder.append("keyword=").append(keyword);

        }
        if(searchParam.getCategory1Id()!=null){

            builder.append("category1Id=").append(searchParam.getCategory1Id());

        }
        if(searchParam.getCategory2Id()!=null){

            builder.append("category2Id=").append(searchParam.getCategory2Id());

        }
        if(searchParam.getCategory3Id()!=null){

            builder.append("category3Id=").append(searchParam.getCategory3Id());

        }

        //品牌
         if(!StringUtils.isEmpty(searchParam.getTrademark())){

             //判断
             if(builder.length()>0){

                 builder.append("&trademark=").append(searchParam.getTrademark());
             }


         }

         //平台属性
        String[] props = searchParam.getProps();
         //props=23:3G:运行内存&props=24:256G:机身内存
        //判断
        if(props!=null &&props.length>0){

            for (String prop : props) {

                if(builder.length()>0) {
                    builder.append("&props=").append(prop);

                }

                }


        }


        return "list.html?"+builder.toString();
    }
}
